/**
 * @author Pratik Joshi
 */
package server;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class dpssUDP extends Thread{
	String serverName; 
	int portNo;
	GameServer tempServer = null;
	
	dpssUDP(String serverName, int portNo, GameServer server)
	{
		this.serverName = serverName;
		this.portNo = portNo;
		tempServer = server;
	}
	
	public void run() {
		
		System.out.println("UDP Server started : "+serverName);
		DatagramSocket host = null;
		try{
			
			host = new DatagramSocket(portNo);
			
			byte [] receiveData = new byte [1024];
			byte [] sendData = new byte [1024];
			
			while(true)
			{
				DatagramPacket request = new DatagramPacket(receiveData, receiveData.length);
				host.receive(request);
				int noOfRecords = tempServer.getlocalCountonline();
				int gn = tempServer.getlocalCountoffline();
                sendData=(serverName+":"+" "+noOfRecords+" "+"Online"+", "+gn+" "+"Offline").getBytes();
				DatagramPacket reply = new DatagramPacket(sendData, sendData.length,request.getAddress(),request.getPort());
				host.send(reply);
			}
			
		}catch(SocketException e){System.out.println("Socket "+e.getMessage());
		}catch(Exception e){System.out.println(e.getMessage());
		}finally {if(host != null){host.close();}}
		
	}

}
